package cn.edu.zafu.bigdata.yt.wlbc;

import java.io.*;
import java.net.*;

public class ChatClient_yt {
    public static void main(String[] args) {
        String serverAddress = "127.0.0.1"; 
        int port = 12345; 

        try (Socket socket = new Socket(serverAddress, port)) {
            System.out.println("Connected to the server!");

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in));

            String userMessage;
            while (true) {
                System.out.print("Enter message (type 'bye' to quit): ");
                userMessage = consoleInput.readLine();

                out.println(userMessage); 
                if ("bye".equalsIgnoreCase(userMessage)) {
                    System.out.println("Disconnected from server.");
                    break;
                }

                String serverResponse = in.readLine();
                System.out.println("Server: " + serverResponse);
            }
        } catch (IOException e) {
            System.out.println("Client error: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

